<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserAbility extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_abilities';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', 'breaking_ice', 'empathy', 'conversation_flow', 'is_baseline'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'is_baseline' => 'boolean',
        'created_at' => 'datetime',
    ];

    /**
     * 所属用户关系
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * 获取能力雷达图数据
     */
    public function getRadarData()
    {
        return [
            'breaking_ice' => $this->breaking_ice,
            'empathy' => $this->empathy,
            'conversation_flow' => $this->conversation_flow,
        ];
    }

    /**
     * 计算总体能力分数
     */
    public function getTotalScore()
    {
        return round(($this->breaking_ice + $this->empathy + $this->conversation_flow) / 3, 1);
    }

    /**
     * 获取最弱的能力维度
     */
    public function getWeakestAbility()
    {
        $abilities = [
            'breaking_ice' => $this->breaking_ice,
            'empathy' => $this->empathy,
            'conversation_flow' => $this->conversation_flow,
        ];

        return array_keys($abilities, min($abilities))[0];
    }
}